package com.outdoor.server.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.outdoor.pojo.vo.TrackVO;
import com.outdoor.server.mapper.NodeAttachmentMapper;
import com.outdoor.server.mapper.NodeMapper;
import com.outdoor.server.mapper.TrackMapper;
import com.outdoor.pojo.dto.NodeCreateDTO;
import com.outdoor.pojo.entity.Node;
import com.outdoor.pojo.entity.Track;
import com.outdoor.server.service.TrackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class TrackServiceImpl implements TrackService {

    @Autowired
    private TrackMapper trackMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private NodeAttachmentMapper nodeAttachmentMapper;

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 开始记录轨迹
     * @param uid 用戶ID
     * @return
     */
    @Override
    @Transactional
    public Long startTrack(Long uid) {
        Track track = new Track();
        track.setUid(uid);
        track.setStartTime(LocalDateTime.now());
        track.setDistance(0.0);
        
        trackMapper.insert(track);
        return track.getId();
    }

    /**
     * 上传节点信息
     * @param nodeCreateDTO 节点信息
     */
    @Override
    @Transactional
    public void addNode(NodeCreateDTO nodeCreateDTO) {
        // 创建节点
        Node node = new Node();
        BeanUtils.copyProperties(nodeCreateDTO, node);
        
        nodeMapper.insert(node);

        //
        Track track = trackMapper.selectById(nodeCreateDTO.getTrackId());
        if (track != null) {
            String nodeSequence = track.getNode();
            if (nodeSequence == null || nodeSequence.isEmpty()) {
                nodeSequence = String.valueOf(node.getId());
            } else {
                nodeSequence += "," + node.getId();
            }
            track.setNode(nodeSequence);
            trackMapper.updateById(track);
        }
    }

    /**
     * 结束记录轨迹
     * @param trackId 轨迹ID
     * @return
     */
    @Override
    @Transactional
    public void endTrack(Long trackId) {
        Track track = trackMapper.selectById(trackId);
        if (track == null) {
            throw new RuntimeException("轨迹不存在");
        }
        
        track.setEndTime(LocalDateTime.now());
        track.setName("户外运动"+LocalDateTime.now());
        
//        // 计算总里程和海拔信息
//        calculateTrackStats(track);
        trackMapper.updateById(track);
    }

    /**
     * 获取轨迹详情
     * @param trackId 轨迹ID
     * @return
     */
    @Override
    public TrackVO getTrackDetail(Long trackId) {
        Track track = trackMapper.selectById(trackId);
        if (track == null) {
            throw new RuntimeException("轨迹不存在");
        }

        TrackVO trackVO = new TrackVO();
        BeanUtils.copyProperties(track, trackVO);

        // 解析节点信息
        String nodeSequence = track.getNode();
        if (nodeSequence != null && !nodeSequence.isEmpty()) {
            String[] nodeIds = nodeSequence.split(",");
            for (String nodeId : nodeIds) {
                Node node = nodeMapper.selectById(Long.valueOf(nodeId));
                if (node != null) {
                    trackVO.getNodes().add(node);
                }
            }
        }

        return trackVO;
    }

    @Override
    public List<Track> getAllTrack(Long uid) {
        return trackMapper.selectAllTrackByUid(uid);
    }

//    private String uploadFile(MultipartFile file) {
//        // TODO: 實現文件上傳邏輯
//        return "";
//    }
//
//    private Integer getFileType(String contentType) {
//        if (contentType.startsWith("image/")) {
//            return 1;
//        } else if (contentType.startsWith("video/")) {
//            return 2;
//        }
//        return 0;
//    }
//
//    private void calculateTrackStats(track track) {
//        // TODO: 實現軌跡統計信息計算邏輯
//        // 1. 計算總里程
//        // 2. 計算最高/最低海拔
//        // 3. 更新節點序列
//    }
} 